//
//  ZJSEmptyStateView.m
//  ZJS
//
//  Created by wangfang on 16/10/27.
//  Copyright © 2016年 zall. All rights reserved.
//  空态页的封装View

#import "ZJSEmptyStateView.h"
#import "NSAttributedString+ZallAdditions.h"

@interface ZJSEmptyStateView ()

/**
 * 空态页图片
 */
@property (nonatomic,strong) UIImageView *emptyImageView;

/**
 * 空态页文字Label
 */
@property (nonatomic,strong) UILabel *emptyLabel;

@end
/**
 * 空态图片的宽度/高度
 */
static CGFloat const emptyImageWidth = 100.0f ;

/**
 * 空态图片和空态文字之间的间距
 */
static CGFloat const emptyMarginBetweenImageViewAndLabel = 20.0f ;

/**
 * 空态文案和空态按钮之间的间距
 */

static CGFloat const emptyMarginBetweenLabelAndButton = 20.0f ;//2017-03-04应UI胡雁要求修改

/**
 * 空态按钮的宽度
 */
static CGFloat const emptyButtonWidth = 130.0f ;

/**
 * 空态按钮的高度
 */
static CGFloat const emptyButtonHeight = 36.0f ;

/**
 * 空态页换行时第一行与第二行之间的间距
 */
static CGFloat const emptyLabelGap = 8.0f ;

/**
 * 空态页统一文字字体(有图)
 */
static CGFloat const emptyLabelFont = 15.0f ;

/**
 * 空态页统一文字字体(无图)
 */
static CGFloat const emptyLabelNoPictureFont = 18.0f ;
/**
 * 空态页Label单行高度(有图)
 */
static CGFloat const singleEmptyLabelHeight = 20.0f ;

/**
 * 空态页Label单行高度(无图)
 */
static CGFloat const singleEmptyLabelNoImageHeight = 35.0f ;

/**
 *  无图状态下,空态页文案距离顶部的距离
 */
static CGFloat const noPictureEmptyLabelTopMargin = 50.0f ;
@implementation ZJSEmptyStateView

@synthesize emptyImageView = _emptyImageView;
@synthesize emptyLabel = _emptyLabel;
@synthesize emptyButton = _emptyButton;

#pragma mark- lifecycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.emptyImageView];
        [self addSubview:self.emptyLabel];
        [self addSubview:self.emptyButton];
        //空态页背景色统一
        self.backgroundColor = UIColorFromHexRGB(0xf2f2f5);
    }
    return self;
}

#pragma mark- layoutSubviews
- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.showEmptyButton) {
        //显示空态页按钮
        //空态按钮居中
        self.emptyButton.frame = CGRectMake(0, self.height * 0.5 - emptyButtonHeight, emptyButtonWidth / 375 * ScreenWidth, emptyButtonHeight / 375 * ScreenWidth);
        //空态文字布局
        self.emptyLabel.left = 0;
        self.emptyLabel.height = [self heightOfEmptyLabelWithEmptyDesc:self.emptyDesc];
        self.emptyLabel.bottom = self.emptyButton.top - emptyMarginBetweenLabelAndButton;
        self.emptyLabel.width = self.width;
        //空态图片布局
        self.emptyImageView.left = 0;
        self.emptyImageView.height = emptyImageWidth / 375 * ScreenWidth;
        self.emptyImageView.width = emptyImageWidth / 375 * ScreenWidth;
        self.emptyImageView.bottom = self.emptyLabel.top - emptyMarginBetweenImageViewAndLabel;
    } else {
        //不显示空态页按钮
        if (self.hideEmptyImage) {
            //没有图片情况下
            self.emptyLabel.frame = CGRectMake(0, noPictureEmptyLabelTopMargin , self.width, [self heightOfEmptyLabelWithEmptyDesc:self.emptyDesc]);
        }else{
            //有图片的情况下
            //空态文字
            self.emptyLabel.left = 0;
            self.emptyLabel.height = [self heightOfEmptyLabelWithEmptyDesc:self.emptyDesc];
            self.emptyLabel.width = self.width;
            self.emptyLabel.bottom = self.height * 0.5;
            //空态图片
            self.emptyImageView.left = 0;
            self.emptyImageView.height = self.emptyImageView.width = emptyImageWidth / 375 * ScreenWidth;
            self.emptyImageView.bottom = self.emptyLabel.top - emptyMarginBetweenImageViewAndLabel;
        }
    }
    //三者居中
    self.emptyButton.centerX = self.emptyLabel.centerX = self.emptyImageView.centerX = self.centerX;
}

#pragma mark- getter
//空态页图片
- (UIImageView *)emptyImageView {
    if (!_emptyImageView) {
        _emptyImageView = [[UIImageView alloc] init];
    }
    return _emptyImageView;
}
//空态页文字Label
- (UILabel *)emptyLabel {
    if (!_emptyLabel) {
        _emptyLabel = [[UILabel alloc] init];
        //默认文字显示,字体15,只有文字,显示18
        _emptyLabel.font = [UIFont systemFontOfSize:(self.hideEmptyImage ? emptyLabelNoPictureFont : emptyLabelFont)];
        //文字居中
        _emptyLabel.textAlignment = NSTextAlignmentCenter;
        //颜色
        _emptyLabel.textColor = UIColorFromHexRGB(0x666666);
        _emptyLabel.numberOfLines = 0;
    }
    return _emptyLabel;
}

//空态页按钮
- (UIButton *)emptyButton {
    if (!_emptyButton) {
        _emptyButton = [[UIButton alloc] init];
        _emptyButton.titleLabel.font = [UIFont systemFontOfSize:emptyLabelFont];
        _emptyButton.backgroundColor = [UIColor clearColor];
        _emptyButton.clipsToBounds = YES;
        _emptyButton.layer.cornerRadius = 5;
        _emptyButton.layer.borderWidth = 1.0;
        if (isZJSUser) {
            _emptyButton.layer.borderColor = UIColorFromHexRGB(0xFF6600).CGColor;
            [_emptyButton setTitleColor:UIColorFromHexRGB(0xFF6600) forState:UIControlStateNormal];
        }else if (isZJSDriver || isZJSTransportDriver) {
            _emptyButton.layer.borderColor = UIColorFromHexRGB(0x2eb6aa).CGColor;
            [_emptyButton setTitleColor:UIColorFromHexRGB(0x2eb6aa) forState:UIControlStateNormal];
        }
        [_emptyButton addTarget:self action:@selector(emptyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        //默认隐藏
        _emptyButton.hidden = YES;
    }
    return _emptyButton;
}

#pragma mark- setter
//设置是否显示空态页按钮
- (void)setShowEmptyButton:(BOOL)showEmptyButton {
    [self willChangeValueForKey:@"showEmptyButton"];
    _showEmptyButton = showEmptyButton;
    [self didChangeValueForKey:@"showEmptyButton"];
    _emptyButton.hidden = !showEmptyButton;
}

//设置空态页文案
- (void)setEmptyDesc:(NSString *)emptyDesc {
    [self willChangeValueForKey:@"emptyDesc"];
    _emptyDesc = emptyDesc;
    [self didChangeValueForKey:@"emptyDesc"];
    self.emptyLabel.attributedText = [NSAttributedString getAttributedStringWithString:emptyDesc lineSpace:emptyLabelGap];
    self.emptyLabel.textAlignment = NSTextAlignmentCenter;
    [self setNeedsLayout];
}

//设置空态页富文本文案
- (void)setEmptyAttributDesc:(NSAttributedString *)emptyDesc {
    _emptyDesc = [emptyDesc string];
    self.emptyLabel.attributedText = emptyDesc;
    self.emptyLabel.textAlignment = NSTextAlignmentCenter;
    [self setNeedsLayout];
}

//设置空态页按钮文案
- (void)setEmptyButtonDesc:(NSString *)emptyButtonDesc {
    [self willChangeValueForKey:@"emptyButtonDesc"];
    _emptyButtonDesc = emptyButtonDesc;
    [self didChangeValueForKey:@"emptyButtonDesc"];
    [self.emptyButton setTitle:emptyButtonDesc forState:UIControlStateNormal];
}

//设置空态页图片image
- (void)setEmptyImage:(UIImage *)emptyImage {
    [self willChangeValueForKey:@"emptyImage"];
    _emptyImage = emptyImage;
    [self didChangeValueForKey:@"emptyImage"];
    self.emptyImageView.image = emptyImage;
}

- (void)setHideEmptyImage:(BOOL)hideEmptyImage {
    [self willChangeValueForKey:@"hideEmptyImage"];
    _hideEmptyImage = hideEmptyImage;
    [self didChangeValueForKey:@"hideEmptyImage"];
    self.emptyImageView.hidden = hideEmptyImage
    ;
    self.emptyLabel.font = [UIFont systemFontOfSize:(self.hideEmptyImage ? emptyLabelNoPictureFont : emptyLabelFont)];
    [self setNeedsLayout];
}
#pragma mark- action
/**
 * 点击了空态页按钮
 */
- (void)emptyButtonClick:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(emptyStateView:emptyButton:)]) {
        [_delegate emptyStateView:self emptyButton:sender];
    }
}

#pragma mark- 计算方法
/**
 * 计算Label的高度
 */
- (CGFloat)heightOfEmptyLabelWithEmptyDesc:(NSString *)emptyStr {
    CGFloat emptyLabelHeight = 0;
    //判断是否要换行
    if ([emptyStr ZJSContainsString:@"\n"]) {
        //需要换行
        NSRange range = [emptyStr rangeOfString:@"\n"];
        NSString *firstStr = [emptyStr substringToIndex:range.location];
        NSString *secondStr = [emptyStr substringFromIndex:range.location];
        CGFloat firstWidth =   [self stringSize:firstStr andMaxSize:CGSizeMake(0, (self.hideEmptyImage ? singleEmptyLabelNoImageHeight : singleEmptyLabelHeight)) andFontNum:(self.hideEmptyImage ? emptyLabelNoPictureFont : emptyLabelFont)].width;
        CGFloat secondWidth =   [self stringSize:secondStr andMaxSize:CGSizeMake(0, (self.hideEmptyImage ? singleEmptyLabelNoImageHeight : singleEmptyLabelHeight)) andFontNum:(self.hideEmptyImage ? emptyLabelNoPictureFont : emptyLabelFont)].width;
        CGFloat maxWidth = MAX(firstWidth, secondWidth);
        emptyLabelHeight = [self stringSize:emptyStr andMaxSize:CGSizeMake(maxWidth, 0) andFontNum:(self.hideEmptyImage ? emptyLabelNoPictureFont : emptyLabelFont)].height;
        ZallLog(@"%f",emptyLabelHeight);
        emptyLabelHeight += emptyLabelGap;
    } else {
        //不要换行
        emptyLabelHeight = (self.hideEmptyImage ? singleEmptyLabelNoImageHeight : singleEmptyLabelHeight);
    }
    return emptyLabelHeight;
}

/**
 *  根据传入的字符串和字体计算出字符串的size
 *
 *  @param string  需要计算的字符串
 *  @param maxSize 最大的size
 *  @param fontNum 字符串的字体
 *
 *  @return 字符串占有的size
 */
- (CGSize)stringSize:(NSString *) string andMaxSize:(CGSize)maxSize andFontNum:(CGFloat)fontNum {
    // 最大尺寸
    NSDictionary *dict = @{NSFontAttributeName : [UIFont systemFontOfSize:fontNum] };
    CGRect labelFrame =  [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    // 获取 size并返回
    return labelFrame.size;
}

- (NSAttributedString *)emptyAttributString:(NSString *)title subTile:(NSString *)subTitle {
    NSAttributedString *emptyStr = nil;
    NSDictionary *titleAtt = @{
                                     NSForegroundColorAttributeName:UIColorFromHexRGB(0x666666),
                                     NSFontAttributeName:[UIFont systemFontOfSize:16.0f]};
    NSDictionary *subTitleAtt = @{
                             NSForegroundColorAttributeName:UIColorFromHexRGB(0x666666),
                             NSFontAttributeName:[UIFont systemFontOfSize:14.0f]};
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 8.0f;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSAttributedString *titleAttStr = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n", title] attributes:titleAtt];
    NSAttributedString *subTitleAttStr = [[NSAttributedString alloc] initWithString:subTitle attributes:subTitleAtt];
    NSMutableAttributedString *mutalbleAttStr = [[NSMutableAttributedString alloc] initWithAttributedString:titleAttStr];
    [mutalbleAttStr appendAttributedString:subTitleAttStr];
    [mutalbleAttStr addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, mutalbleAttStr.length)];
    emptyStr = [mutalbleAttStr copy];
    return (emptyStr);
}

@end
